<?php

    Class CargasDAO
    {

        private static function getInstance($rs)
        {
            $carga = new Carga();

            $carga->idCarga   = $rs->fields["idcarga"];
            $carga->idUsuario = $rs->fields["idusuario"];
            $carga->monto     = $rs->fields["monto"];
            $carga->fecha     = $rs->fields["fecha"];

            return $carga;
        }

        static function add(&$carga)
        {
            $db = MyConnection::$db;

            $query = $db->Prepare("INSERT INTO cargas (monto, fecha, idUsuario) VALUES (?,?,?)");
            return $db->Execute($query, array($carga->monto, $carga->fecha, $carga->idUsuario));
        }

        static function getById($id)
        {
            $db = MyConnection::$db;

            $rs = $db->Execute("SELECT * FROM cargas WHERE idCarga = $id");

            if (!$rs->EOF)
            {
                return self::getInstance($rs);
            }
        }

        static function getAll()
        {
            $db = MyConnection::$db;

            $rs = $db->Execute("SELECT * FROM cargas");

            $all = array();
            while (!$rs->EOF)
            {
                $all[] = self::getInstance($rs);
                $rs->MoveNext();
            }

            return $all;
        }

        static function getSumaMontos($idUsuario)
        {
            $db = MyConnection::$db;

            $rs = $db->Execute("SELECT sum(monto) AS suma FROM cargas WHERE idusuario = $idUsuario");

            if (!$rs->EOF)
            {
                return $rs->fields["suma"];
            }
        }
    }

?>